<template>
  <div class="address_container">
    <el-checkbox :indeterminate="isIndeterminate"
      v-model="checkAll"
      @change="handleCheckAllChange">全选</el-checkbox>
      
    <el-button size="small">新建收获地址</el-button>
    <div class="dele">
      <div class="address_card"
        
        :key="index"
        v-for="(item,index) in addressList">
        <div class="defualt_address">
          <span style="margin-right:10px">设为默认地址</span>
          <el-switch v-model="item.mor">
          </el-switch>
        </div>
        <div class="info_address">
          <p>收货人: {{item.aname}}</p>
          <p>所在地区: {{item.address+item.ud1+item.ud2}}</p>
          <p>详细地址: {{item.taddress}}</p>
          <p>手机号: {{item.aphone}}</p>
        </div>
        <div class="bottom_btnGrp">
          <el-button @click="deleAddress(item.aid)">删除</el-button>
          <el-button @click="editAddress(index)">修改</el-button>
        </div>
        <el-checkbox-group v-model="checkedCities"
          @change="handleCheckedCitiesChange">
          <el-checkbox :label="item">
            <div class="tips"
              @click="demo(item,index)"
              ref="tips"><i class="el-icon-check"></i></div>
          </el-checkbox>
        </el-checkbox-group>

      </div>
    </div>

    <!-- 修改地址弹出框 -->
    <el-dialog title="修改地址"
      :visible.sync="dialogVisible"
      width="40%">

      <el-form :model="edit_address"
        label-width="100px"
        class="demo-ruleForm">
        <el-form-item label="收件人">
          <el-input v-model="edit_address.aname"></el-input>
        </el-form-item>

        <el-form-item label="所在地区">
          <el-cascader v-model="edit_address.address"
            :options="options"
            :props="{ expandTrigger: 'hover' }"></el-cascader>
        </el-form-item>

        <el-form-item label="详细地址">
          <el-input v-model="edit_address.taddress"></el-input>
        </el-form-item>

        <el-form-item label="手机号">
          <el-input v-model="edit_address.aphone"></el-input>
        </el-form-item>

        <el-checkbox v-model="edit_address.mor">设为默认地址</el-checkbox>

      </el-form>

      <span slot="footer"
        class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary"
          @click="sumit()">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
//header
import TopHeader from '@/components/modules/header.vue';
//api
import { getUaddresslist, delUaddress, modUaddress } from '@/utils/api/api.js';
export default {
  components: {
    TopHeader,
  },
  data() {
    return {
      checkAll: false,
      checkedCities: [],
      cities: [],
      isIndeterminate: false,
      options: [
        {
          value: '山西省',
          label: '山西省',
          children: [
            {
              value: '阳泉市',
              label: '阳泉市',
              children: [
                {
                  value: '平定县',
                  label: '平定县',
                },
                {
                  value: 'fankui',
                  label: '反馈',
                },
                {
                  value: 'xiaolv',
                  label: '效率',
                },
                {
                  value: 'kekong',
                  label: '可控',
                },
              ],
            },
            {
              value: 'daohang',
              label: '导航',
              children: [
                {
                  value: 'cexiangdaohang',
                  label: '侧向导航',
                },
                {
                  value: 'dingbudaohang',
                  label: '顶部导航',
                },
              ],
            },
          ],
        },
        {
          value: 'zujian',
          label: '组件',
          children: [
            {
              value: 'basic',
              label: 'Basic',
              children: [
                {
                  value: 'layout',
                  label: 'Layout 布局',
                },
                {
                  value: 'color',
                  label: 'Color 色彩',
                },
                {
                  value: 'typography',
                  label: 'Typography 字体',
                },
                {
                  value: 'icon',
                  label: 'Icon 图标',
                },
                {
                  value: 'button',
                  label: 'Button 按钮',
                },
              ],
            },
            {
              value: 'form',
              label: 'Form',
              children: [
                {
                  value: 'radio',
                  label: 'Radio 单选框',
                },
                {
                  value: 'checkbox',
                  label: 'Checkbox 多选框',
                },
                {
                  value: 'input',
                  label: 'Input 输入框',
                },
                {
                  value: 'input-number',
                  label: 'InputNumber 计数器',
                },
                {
                  value: 'select',
                  label: 'Select 选择器',
                },
                {
                  value: 'cascader',
                  label: 'Cascader 级联选择器',
                },
                {
                  value: 'switch',
                  label: 'Switch 开关',
                },
                {
                  value: 'slider',
                  label: 'Slider 滑块',
                },
                {
                  value: 'time-picker',
                  label: 'TimePicker 时间选择器',
                },
                {
                  value: 'date-picker',
                  label: 'DatePicker 日期选择器',
                },
                {
                  value: 'datetime-picker',
                  label: 'DateTimePicker 日期时间选择器',
                },
                {
                  value: 'upload',
                  label: 'Upload 上传',
                },
                {
                  value: 'rate',
                  label: 'Rate 评分',
                },
                {
                  value: 'form',
                  label: 'Form 表单',
                },
              ],
            },
            {
              value: 'data',
              label: 'Data',
              children: [
                {
                  value: 'table',
                  label: 'Table 表格',
                },
                {
                  value: 'tag',
                  label: 'Tag 标签',
                },
                {
                  value: 'progress',
                  label: 'Progress 进度条',
                },
                {
                  value: 'tree',
                  label: 'Tree 树形控件',
                },
                {
                  value: 'pagination',
                  label: 'Pagination 分页',
                },
                {
                  value: 'badge',
                  label: 'Badge 标记',
                },
              ],
            },
            {
              value: 'notice',
              label: 'Notice',
              children: [
                {
                  value: 'alert',
                  label: 'Alert 警告',
                },
                {
                  value: 'loading',
                  label: 'Loading 加载',
                },
                {
                  value: 'message',
                  label: 'Message 消息提示',
                },
                {
                  value: 'message-box',
                  label: 'MessageBox 弹框',
                },
                {
                  value: 'notification',
                  label: 'Notification 通知',
                },
              ],
            },
            {
              value: 'navigation',
              label: 'Navigation',
              children: [
                {
                  value: 'menu',
                  label: 'NavMenu 导航菜单',
                },
                {
                  value: 'tabs',
                  label: 'Tabs 标签页',
                },
                {
                  value: 'breadcrumb',
                  label: 'Breadcrumb 面包屑',
                },
                {
                  value: 'dropdown',
                  label: 'Dropdown 下拉菜单',
                },
                {
                  value: 'steps',
                  label: 'Steps 步骤条',
                },
              ],
            },
            {
              value: 'others',
              label: 'Others',
              children: [
                {
                  value: 'dialog',
                  label: 'Dialog 对话框',
                },
                {
                  value: 'tooltip',
                  label: 'Tooltip 文字提示',
                },
                {
                  value: 'popover',
                  label: 'Popover 弹出框',
                },
                {
                  value: 'card',
                  label: 'Card 卡片',
                },
                {
                  value: 'carousel',
                  label: 'Carousel 走马灯',
                },
                {
                  value: 'collapse',
                  label: 'Collapse 折叠面板',
                },
              ],
            },
          ],
        },
        {
          value: 'ziyuan',
          label: '资源',
          children: [
            {
              value: 'axure',
              label: 'Axure Components',
            },
            {
              value: 'sketch',
              label: 'Sketch Templates',
            },
            {
              value: 'jiaohu',
              label: '组件交互文档',
            },
          ],
        },
      ],
      user: [],
      //地址列表
      addressList: [],
      //弹出框状态
      dialogVisible: false,
      //当前编辑地址
      edit_address: {
        //当前用户
        uid: '',
        //收件人
        aname: '',
        //手机号
        aphone: '',
        //所在地址
        address: {},
        //详细地址

        //默认地址
        mor: false,
      },
    };
  },
  methods: {
    demo(item, index) {
      setTimeout(() => {
        if (this.checkedCities.length > 0) {
          this.checkedCities.forEach(obj => {
            if (obj.aid == item.aid) {
              this.$refs.tips[index].style = 'background-color:orange';
            } else {
              this.$refs.tips[index].style =
                'background-color:rgba(0, 0, 0, 0.2)';
            }
          });
        } else {
          this.$refs.tips[index].style = 'background-color:rgba(0, 0, 0, 0.2)';
        }
      }, 100);
    },

    handleCheckAllChange(val) {
      this.checkedCities = val ? this.addressList : [];
      this.isIndeterminate = false;
      if (val) {
        this.$refs.tips.forEach(item => {
          item.style = 'background-color:orange';
        });
      } else {
        this.$refs.tips.forEach(item => {
          item.style = 'background-color:rgba(0, 0, 0, 0.2)';
        });
      }
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.addressList.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.addressList.length;
    },

    //获取地址
    getAddressList() {
      getUaddresslist(this.user.uid).then(resp => {
        this.addressList = resp.sku;
        console.log(this.addressList);
      });
    },
    //删除地址
    deleAddress(val) {
      delUaddress(val).then(resp => {
        if (resp.code == 200) {
          this.getAddressList();
        }
      });
    },
    //修改地址
    editAddress(idx) {
      this.edit_address = this.addressList[idx];
      console.log(this.edit_address);
      this.dialogVisible = true;
    },
    //提交修改
    sumit() {
      modUaddress(this.edit_address).then(resp => {
        this.dialogVisible = false;
      });
    },
  },
  created() {
    this.currentGoodsId = this.$route.query.goodsId;
    this.user = JSON.parse(localStorage.getItem('user'));
    this.edit_address.uid = this.user.uid;
    this.getAddressList();
  },
};
</script>

<style lang="scss" scoped>
.dele {
  width: 100%;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-flow: row wrap;

  .address_card {
    width: 45%;
    display: flex;
    flex-direction: column;
    border: 1px solid #ececec;
    margin: 20px 20px 20px 0;
    overflow: hidden;
    position: relative;

    .defualt_address {
      box-sizing: border-box;
      padding: 10px;
      display: flex;
      width: 100%;
      justify-content: right;
    }
    .info_address {
      box-sizing: border-box;
      padding: 10px;
      p {
        padding: 6px 0;
        color: #727272;
        font-family: 'Helvetica Neue', Helvetica, 'PingFang SC',
          'Hiragino Sans GB', 'Microsoft YaHei', '微软雅黑', Arial, sans-serif;
      }
    }
    .bottom_btnGrp {
      display: flex;
      justify-content: space-between;
      .el-button {
        width: 50%;
        margin: 0;
      }
    }
    .tips {
      position: absolute;
      right: -480px;
      bottom: -34px;
      height: 100px;
      width: 100px;
      font-size: 30px;
      background-color: rgba(0, 0, 0, 0.2);
      text-align: center;
      transform: rotate(135deg);
      .el-icon-check {
        position: absolute;
        right: 31px;
        bottom: 3px;
        color: #ffffff;
        transform: rotate(-135deg);
        font-size: 20px;
        font-weight: bolder;
      }
    }
  }
}
:deep(.el-checkbox-group) {
  height: 0;
}
:deep(.el-checkbox-group .el-checkbox__input) {
  display: none;
}
</style>